from django.shortcuts import render,HttpResponse,redirect
from  .models import *
from django.core.paginator import Paginator
# Create your views here.
def index(request):
    return HttpResponse('ok')

# 注册
def reg(request):
    if request.method == 'GET':
        return render(request,'reg.html')
    if request.method == 'POST':
        uname = request.POST.get('uname')
        upwd = request.POST.get('upwd')
        upwd2 = request.POST.get('upwd2')
        user = Users.objects.filter(uname=uname).first()
        if user:
            return HttpResponse('账号已被注册')
        elif upwd != upwd2:
            return HttpResponse('两次输入密码不一致')
        role = request.POST.get('role')
        Users.objects.create(uname=uname,upwd=upwd,role=role)
        return  redirect('app1:login')

# 登录
def login(request):
    if request.method == 'GET':
        return render(request,'login.html')
    if request.method == 'POST':
        uname = request.POST.get('uname')
        upwd = request.POST.get('upwd')
        user = Users.objects.filter(uname=uname,upwd=upwd).first()
        if user:
            if user.role == 1:
                request.session['uname'] = uname
                request.session['role'] = user.role
                return  redirect('app1:add')
            else:
                request.session['uname'] = uname
                request.session['role'] = user.role
                return redirect('app1:show')
        else:
            return HttpResponse('用户名或者密码错误')

# 添加假条
def addleave(request):

    # 判断是否登录
    if not request.session.get('uname'):
        return redirect('app1:login')

    # 判断用户角色信息
    if request.session.get('role') != 1:
        response = HttpResponse('没有权限,拒绝访问', status=403, charset='utf-8')
        response['content_type'] = 'text/html'
        return response

    if request.method == 'GET':
        type = Type.objects.all()
        return render(request,'add.html',{'type':type})
    if request.method == 'POST':
        stu_name = request.POST.get('stu_name')
        content = request.POST.get('content')
        time = request.POST.get('time')
        tid_id = request.POST.get('tid_id')
        Leave.objects.create(stu_name=stu_name,time=time,tid_id=tid_id,content=content)
        return HttpResponse('添加成功')

# 展示假条
def showl(request):
    # 判断是否登录
    if not request.session.get('uname'):
        return redirect('app1:login')

    # 判断用户角色信息
    if request.session.get('role') != 2:
        response = HttpResponse('没有权限,拒绝访问', status=403, charset='utf-8')
        response['content_type'] = 'text/html'
        return response

    leave_type = Type.objects.all()
    status = request.GET.get('status', '-1')
    if status == '-1':
        leaves = Leave.objects.all()
    else:
        leaves = Leave.objects.filter(tid_id=int(status)).all()
    status = int(status)
    page = request.GET.get('page', 1)
    pageint = int(page)
    pagesize = 2
    pageinator = Paginator(leaves, pagesize)
    pagex = pageinator.page(pageint)
    leaves = pagex.object_list
    return render(request, 'show.html',
                  {"leave_type": leave_type, 'status': status, 'page': pageint, 'leaves': leaves})

# 通过
def passs(request,id):
    Leave.objects.filter(id=id).update(status=1)
    return redirect('app1:show')

# 不通过
def pas(request,id):
    Leave.objects.filter(id=id).update(status=2)
    return redirect('app1:show')